package com.ruoyi.project.yingyu.bookcatelog.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.yingyu.bookcatelog.domain.BookCatelog;
import com.ruoyi.project.yingyu.bookcatelog.service.IBookCatelogService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 单词目录Controller
 * 
 * @author shixiangwen
 * @date 2020-03-23
 */
@Controller
@RequestMapping("/yingyu/bookcatelog")
public class BookCatelogController extends BaseController
{
    private String prefix = "yingyu/bookcatelog";

    @Autowired
    private IBookCatelogService bookCatelogService;

    @RequiresPermissions("yingyu:bookcatelog:view")
    @GetMapping()
    public String bookcatelog()
    {
        return prefix + "/bookcatelog";
    }

    /**
     * 查询单词目录树列表
     */
    @RequiresPermissions("yingyu:bookcatelog:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BookCatelog> list(BookCatelog bookCatelog)
    {
        List<BookCatelog> list = bookCatelogService.selectBookCatelogList(bookCatelog);
        return list;
    }

    /**
     * 导出单词目录列表
     */
    @RequiresPermissions("yingyu:bookcatelog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BookCatelog bookCatelog)
    {
        List<BookCatelog> list = bookCatelogService.selectBookCatelogList(bookCatelog);
        ExcelUtil<BookCatelog> util = new ExcelUtil<BookCatelog>(BookCatelog.class);
        return util.exportExcel(list, "bookcatelog");
    }

    /**
     * 新增单词目录
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bookCatelog", bookCatelogService.selectBookCatelogById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存单词目录
     */
    @RequiresPermissions("yingyu:bookcatelog:add")
    @Log(title = "单词目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BookCatelog bookCatelog)
    {
        return toAjax(bookCatelogService.insertBookCatelog(bookCatelog));
    }

    /**
     * 修改单词目录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BookCatelog bookCatelog = bookCatelogService.selectBookCatelogById(id);
        mmap.put("bookCatelog", bookCatelog);
        return prefix + "/edit";
    }

    /**
     * 修改保存单词目录
     */
    @RequiresPermissions("yingyu:bookcatelog:edit")
    @Log(title = "单词目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BookCatelog bookCatelog)
    {
        return toAjax(bookCatelogService.updateBookCatelog(bookCatelog));
    }

    /**
     * 删除
     */
    @RequiresPermissions("yingyu:bookcatelog:remove")
    @Log(title = "单词目录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(bookCatelogService.deleteBookCatelogById(id));
    }

    /**
     * 选择单词目录树
     */
    @GetMapping(value = { "/selectBookcatelogTree/{id}", "/selectBookcatelogTree/" })
    public String selectBookcatelogTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bookCatelog", bookCatelogService.selectBookCatelogById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载单词目录树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = bookCatelogService.selectBookCatelogTree();
        return ztrees;
    }
}
